home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIMETA.PAK / FILE.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  12KB  |  470 lines

  1. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  2. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  3. // PARTICULAR PURPOSE.
  4. //
  5. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  6. //
  7. //  MODULE: file.c
  8. //
  9. //  PURPOSE: File manipulation functions.
  10. //
  11. //  FUNCTIONS:
  12. //    CmdFileNew       - Create a new text document.
  13. //    CmdFileSave      - Save the current document.
  14. //    Open             - Open a file based on a file name.
  15. //    QuerySaveFile    - Prompt and save the current document.
  16. //    SaveFile         - Save the metafile to the current file.
  17. //    SaveAs           - Save the metafile to the specified file.
  18. //    MapFileReadOnly  - Opens a file for reading and map it into memory.
  19. //    MapFileReadWrite - Opens a file for read/write and map it into memory.
  20. //    CloseFileMapping - Unmaps and closes a file mapping.
  21. //
  22. //  COMMENTS:
  23. //
  24.  
  25.  
  26. #include <windows.h>            // required for all Windows applications
  27. #include <windowsx.h>
  28. #include <commctrl.h>
  29. #include "globals.h"            // prototypes specific to this application
  30. #include "resource.h"
  31. #include <string.h>             // for strncmpi
  32. #include "palette.h"            // palette support functions
  33. #include "metafile.h"           // metafile support functions
  34.  
  35.  
  36. // Local function prototypes
  37.  
  38. BOOL SaveFile(HWND);
  39.  
  40.  
  41. // Global variables
  42.  
  43. char szFName[256];
  44. char szUntitled[] = SZAPPNAME" - (untitled)";
  45. BOOL bModify = FALSE;
  46. HENHMETAFILE hEMF = NULL;
  47.  
  48.  
  49. //
  50. //  FUNCTION: CmdFileNew(HWND, WORD, WORD, HWND)
  51. //
  52. //  PURPOSE: Create a new text document.
  53. //
  54. //  PARAMETERS:
  55. //    hwnd     - The window.
  56. //    wCommand - IDM_FILENEW (unused)
  57. //    wNotify  - Notification number (unused)
  58. //    hwndCtrl - NULL (unused)
  59. //
  60. //  RETURN VALUE:
  61. //    Always returns 0 - command handled.
  62. //
  63. //  COMMENTS:
  64. //
  65. //
  66.  
  67. #pragma argsused
  68. LRESULT CmdFileNew(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  69. {
  70.     // If current file has been modified, query user about saving it.
  71.     if (!QuerySaveFile(hwnd))
  72.         return 0;
  73.  
  74.     if (NULL != hEMF)
  75.     {
  76.         DeleteEnhMetaFile(hEMF);
  77.         hEMF = NULL;
  78.     }
  79.     bModify = FALSE;
  80.     GetFName()[0] = 0;
  81.     SetWindowText(hwnd, szUntitled);
  82.  
  83.     // Tell client window...
  84.     ClientNewDrawing();
  85.  
  86.     return 0;
  87. }
  88.  
  89. //
  90. //  FUNCTION: CmdFileSave(HWND, WORD, WORD, HWND)
  91. //
  92. //  PURPOSE: Save the current document.
  93. //
  94. //  PARAMETERS:
  95. //    hwnd     - The window.
  96. //    wCommand - IDM_FILESAVE (unused)
  97. //    wNotify  - Notification number (unused)
  98. //    hwndCtrl - NULL (unused)
  99. //
  100. //  RETURN VALUE:
  101. //    Always returns 0 - command handled.
  102. //
  103. //  COMMENTS:
  104. //
  105. //
  106.  
  107. #pragma argsused
  108. LRESULT CmdFileSave(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  109. {
  110.      // If there is no filename, use the saveas command to get one.
  111.      //   Otherwise, save the file using the current filename.
  112.     if (!GetFName()[0])
  113.     {
  114.         CmdFileSaveAs(hwnd, wCommand, 0, hwndCtrl);
  115.     }
  116.     else
  117.     {
  118.         // Only save the document if it has been modified.
  119.         if (bModify && SaveFile(hwnd))
  120.             bModify = FALSE;
  121.     }
  122.  
  123.      return 0;
  124. }
  125.  
  126. //
  127. //  FUNCTION: Open(LPSTR, BOOL, HWND)
  128. //
  129. //  PURPOSE: Open a file based on a file name and readonly attribute.
  130. //
  131. //  PARAMETERS:
  132. //    lsz - The full path name of the file to be opened.
  133. //    fReadOnly - Read only attribute of the file. (ignored in GDIMeta)
  134. //    hwnd - The window into which the file is being opened.
  135. //
  136. //  RETURN VALUE:
  137. //    NONE
  138. //
  139. //  COMMENTS:
  140. //
  141. //
  142.  
  143. #pragma argsused
  144. VOID Open(LPSTR lsz, BOOL fReadOnly, HWND hwnd)
  145. {
  146.     char rgch[256];
  147.  
  148.     if (NULL != hEMF)                       // Delete previous metafile
  149.         DeleteEnhMetaFile(hEMF);
  150.  
  151.     hEMF = LoadMetaFile(lsz);               // Load new metafile
  152.  
  153.     if (NULL == hEMF)                       // Success?
  154.     {
  155.         MessageBox(hwnd,
  156.                    "Not a valid MetaFile.",
  157.                          NULL,
  158.                    MB_OK | MB_ICONINFORMATION);
  159.         return;
  160.     }
  161.  
  162.     // Set new window title
  163.     wsprintf(rgch, "%s - %s", szAppName, lsz);
  164.     SetWindowText(hwnd, rgch);
  165.  
  166.     bModify = FALSE;                        // Just opened ==> not modified
  167.     lstrcpy(GetFName(), lsz);               // Save file name
  168.  
  169.      // Tell client window that we've opened a metafile
  170.     ClientNewDrawing();
  171. }
  172.  
  173. //
  174. //  FUNCTION: QuerySaveFile(HWND)
  175. //
  176. //  PURPOSE: Save the current document if necessary & the user agrees.
  177. //
  178. //  PARAMETERS:
  179. //    hwnd - The document window.
  180. //
  181. //  RETURN VALUE:
  182. //    TRUE  - The file has been saved, or any changes can be thrown out.
  183. //    FALSE - The use has changed his/her mind, don't continue the
  184. //      operation that would throw away this document.
  185. //
  186. //  COMMENTS:
  187. //
  188. //
  189.  
  190. BOOL QuerySaveFile(HWND hwnd)
  191. {
  192.     int  idResponse;
  193.     char rgch[256];
  194.  
  195.     if (bModify)  // Are there any changes to save?
  196.     {
  197.         wsprintf(rgch, "Save current changes? %s", GetFName());
  198.         idResponse = MessageBox(hwnd,
  199.                                 rgch,
  200.                                 "EditFile",
  201.                                 MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  202.  
  203.         if (idResponse == IDYES)            // Save changes
  204.         {
  205.             // Make sure there is a filename to save to
  206.             if (!GetFName()[0])
  207.                 return (BOOL)CmdFileSaveAs(hwnd, IDM_FILESAVEAS, 0, NULL);
  208.             else
  209.                 SaveFile(hwnd);
  210.         }
  211.         else if (idResponse == IDCANCEL)    // Cancel entire operation
  212.             return FALSE;
  213.     }
  214.  
  215.     return TRUE;                            // Proceed
  216. }
  217.  
  218. //
  219. //  FUNCTION: SaveFile(HWND)
  220. //
  221. //  PURPOSE: Save the MetaFile to the current file.
  222. //
  223. //  PARAMETERS:
  224. //    hwnd - The main application window.
  225. //
  226. //  RETURN VALUE:
  227. //    TRUE - Sucessfully saved the file.
  228. //    FALSE - Unable to save the file.
  229. //
  230. //  COMMENTS:
  231. //
  232.  
  233. #pragma argsused
  234. BOOL SaveFile(HWND hwnd)
  235. {
  236.      HENHMETAFILE hemfTemp = GetCurrentEMF();
  237.  
  238.     // First make sure we have a metafile and a file name
  239.  
  240.     if (!GetFName()[0] || NULL == hemfTemp)
  241.         return FALSE;
  242.  
  243.     // Delete old metafile and save new metafile handle in hEMF
  244.  
  245.     if (hEMF)
  246.         DeleteEnhMetaFile(hEMF);
  247.  
  248.      hEMF = hemfTemp;
  249.  
  250.  
  251.     // Check file extension for file type
  252.     // "WMF" ==> placeable format, otherwise default to enhanced format
  253.  
  254.     if (0 == strncmpi(GetFName() + lstrlen(GetFName()) - 4, ".wmf", 4))
  255.     {
  256.         // Save as a placeable metafile.
  257.         if (!SavePlaceableMetaFile(hEMF, GetFName()))
  258.             return FALSE;
  259.     }
  260.      else
  261.     {
  262.         // Save in enhanced metafile format... let the system
  263.         // do all the work of saving the metafile.
  264.  
  265.         hemfTemp = CopyEnhMetaFile(hEMF, GetFName());
  266.         if (NULL == hemfTemp)
  267.             return FALSE;
  268.  
  269.         DeleteEnhMetaFile(hemfTemp);
  270.     }
  271.  
  272.      // Success!
  273.     bModify = FALSE;
  274.     return TRUE;
  275. }
  276.  
  277.  
  278. //
  279. //  FUNCTION: SaveAs(LPSTR, HWND)
  280. //
  281. //  PURPOSE: Save the edit text to the specified file.
  282. //
  283. //  PARAMETERS:
  284. //    lsz  - The fully qualified path of the file to save.
  285. //    hwnd - The main application window.
  286. //
  287. //  RETURN VALUE:
  288. //    NONE
  289. //
  290. //  COMMENTS:
  291. //
  292. //
  293.  
  294. VOID SaveAs(LPSTR lsz, HWND hwnd)
  295. {
  296.     char rgch[256];
  297.  
  298.     lstrcpy(rgch, GetFName());
  299.     lstrcpy(GetFName(), lsz);
  300.     if (SaveFile(hwnd))
  301.     {
  302.         wsprintf(rgch, "%s - %s", szAppName, GetFName());
  303.         SetWindowText(hwnd, rgch);
  304.     }
  305.     else
  306.         lstrcpy(GetFName(), rgch);
  307. }
  308.  
  309.  
  310. //
  311. //  FUNCTION: MapFileReadOnly(LPCSTR, LPCSTR, PFILEMAP)
  312. //
  313. //  PURPOSE: Open a file for reading and map it into memory.
  314. //
  315. //  PARAMETERS:
  316. //    szFile    - The fully qualified path of the file to open.
  317. //    szMapName - The name of the file mapping object to create.
  318. //    pfm       - Points to a FILEMAP structure that receives the file
  319. //                handle, the file mapping object's handle, and the
  320. //                pointer to the view of the file in memory.
  321. //
  322. //  RETURN VALUE:
  323. //    TRUE for success, FALSE otherwise.
  324. //
  325. //  COMMENTS:
  326. //
  327. //
  328.  
  329. BOOL MapFileReadOnly(LPCSTR szFile, LPCSTR szMapName, PFILEMAP pfm)
  330. {
  331.     //
  332.     // Open the file
  333.     //
  334.     pfm->hFile = CreateFile(szFile,
  335.                             GENERIC_READ,
  336.                             FILE_SHARE_READ,
  337.                             NULL,
  338.                             OPEN_EXISTING,
  339.                             FILE_ATTRIBUTE_READONLY,
  340.                             NULL);
  341.  
  342.     if (pfm->hFile == (HANDLE)-1)
  343.         return FALSE;
  344.  
  345.     //
  346.     // Create a file mapping of the opened file
  347.     //
  348.     pfm->hFileMap = CreateFileMapping(pfm->hFile,
  349.                                       NULL,
  350.                                       PAGE_READONLY,
  351.                                       0, 0,
  352.                                       szMapName);
  353.     if (pfm->hFileMap == NULL)
  354.     {
  355.         CloseHandle(pfm->hFile);
  356.         return FALSE;
  357.     }
  358.  
  359.     //
  360.     // Map a view of the whole file
  361.     //
  362.     pfm->pFileMap = MapViewOfFile(pfm->hFileMap, FILE_MAP_READ, 0, 0, 0);
  363.     if (pfm->pFileMap == NULL)
  364.     {
  365.         CloseHandle(pfm->hFileMap);
  366.         CloseHandle(pfm->hFile);
  367.         return FALSE;
  368.     }
  369.  
  370.     return TRUE;
  371. }
  372.  
  373.  
  374. //
  375. //  FUNCTION: MapFileReadWrite(LPCSTR, LPCSTR, PFILEMAP, DWORD, DWORD)
  376. //
  377. //  PURPOSE: Open a file for reading and writing and map it into memory.
  378. //
  379. //  PARAMETERS:
  380. //    szFile    - The fully qualified path of the file to open.
  381. //    szMapName - The name of the file mapping object to create.
  382. //    pfm       - Points to a FILEMAP structure that receives the file
  383. //                handle, the file mapping object's handle, and the
  384. //                pointer to the view of the file in memory.
  385. //    dwCreateFlags - File creation flags passed to CreateFile as fdwCreate
  386. //    dwMaxSize - The size of the file mapping object.
  387. //
  388. //  RETURN VALUE:
  389. //    TRUE for success, FALSE otherwise.
  390. //
  391. //  COMMENTS:
  392. //
  393. //
  394.  
  395. BOOL MapFileReadWrite(LPCSTR   szFile,
  396.                       LPCSTR   szMapName,
  397.                       PFILEMAP pfm,
  398.                       DWORD    dwCreateFlags, // e.g. OPEN_EXISTING
  399.                       DWORD    dwMaxSize)
  400. {
  401.     //
  402.     // Open the file
  403.     //
  404.     pfm->hFile = CreateFile(szFile,
  405.                             GENERIC_READ | GENERIC_WRITE,
  406.                             0,
  407.                             NULL,
  408.                             dwCreateFlags,
  409.                             FILE_ATTRIBUTE_NORMAL,
  410.                             NULL);
  411.  
  412.     if (pfm->hFile == (HANDLE)-1)
  413.         return FALSE;
  414.  
  415.     //
  416.     // Create a file mapping of the opened file
  417.     //
  418.     pfm->hFileMap = CreateFileMapping(pfm->hFile,
  419.                                       NULL,
  420.                                       PAGE_READWRITE,
  421.                                       0, dwMaxSize,
  422.                                       szMapName);
  423.     if (pfm->hFileMap == NULL)
  424.     {
  425.         CloseHandle(pfm->hFile);
  426.         return FALSE;
  427.     }
  428.  
  429.     //
  430.     // Map a view of the whole file
  431.     //
  432.     pfm->pFileMap = MapViewOfFile(pfm->hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, dwMaxSize);
  433.     if (pfm->pFileMap == NULL)
  434.     {
  435.         CloseHandle(pfm->hFileMap);
  436.         CloseHandle(pfm->hFile);
  437.         return FALSE;
  438.     }
  439.  
  440.     return TRUE;
  441. }
  442.  
  443.  
  444. //
  445. //  FUNCTION: CloseFileMapping(PFILEMAP)
  446. //
  447. //  PURPOSE: Unmaps and closes a file previously opened with MapFileReadOnly
  448. //    or MapFileReadWrite.
  449. //
  450. //  PARAMETERS:
  451. //    pfm       - Points to a FILEMAP structure containing a file
  452. //                handle, a file mapping handle, and the address of
  453. //                the view of the file in memory.
  454. //
  455. //  RETURN VALUE:
  456. //    Always returns TRUE.
  457. //
  458. //  COMMENTS:
  459. //
  460. //
  461.  
  462. BOOL CloseFileMapping(PFILEMAP pfm)
  463. {
  464.     UnmapViewOfFile(pfm->pFileMap);
  465.     CloseHandle(pfm->hFileMap);
  466.     CloseHandle(pfm->hFile);
  467.  
  468.     return TRUE;
  469. }
  470.